package fatboy.addons;


import fatboy.hypeerweb.Node;
/**
 * FinalListener Class is a class to wait for other nodes to give acknowledgements andreports when all have
 * @author Jack
 * @domain all possible positive Integers X all possible Integers smaller or equal to the first.
 */
public class FinalListener extends Listener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4967058661709087533L;
	private int done;
	private int soFar;
	private int myCount;
	/**
	 * a constructor to make the remote proxy for this class work
	 */
	public FinalListener(){
		//exist
	}
	
	/**
	 * Constructor for beginning  broadcast with acknowledgement, 
	 * takes how many things this should wait for to answer, and what node is the origin.
	 * Sets up a listener that will report when the whole web has reported 
	 * @param size the number of listener objects that this listener is listening for
	 * @param origin the origin point
	 * @pre true
	 * @post new listener listening for size number of things if that is zero it 
	 * will already have reported to the the calling application
	 */
	public FinalListener(int size, Node origin) {
		super(1, null);//one so it won't try to report
		done = size;
		soFar = -1;
		myCount = 0;
		inc(0);
	}
	/**
	 * Method to cause a listener to count that one of the lower listeners has reported
	 * and will report if all have reported
	 * @pre a listener this is listening to has just reported all acknowledged
	 * @post number of things reporting one greater and will report 
	 * if it has received acknowledgments from all the listeners below it 
	 */
	public void inc(int count){
		soFar++;
		myCount += count;
		if(done == soFar){
			System.out.println(myCount + " nodes are in the Web.");
		}
	}
	public Object writeReplace(){
		return new ProxyFinalListener(this.getId());
	}
}
